//
//  Network.m
//  Auditory
//
//  Created by 寒竹子 on 15/4/28.
//  Copyright (c) 2015年 寒竹子. All rights reserved.
//

#import "Network.h"
#import "AFHTTPRequestOperationManager.h"

@implementation Network

// 下载JSON数据
+ (void)getJSONWithBaseURL:(NSString *)URL
                    params:(id)params
                returnCall:(ReturnBlock)block
{
    AFHTTPRequestOperationManager * manager = [[AFHTTPRequestOperationManager alloc] init];
    [manager GET:URL parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        // 请求成功
        if (block) {
            block(responseObject, nil);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        // 请求失败
        if (block) {
            block(nil, error);
        }
    }];
}

// 上传JSON数据
+ (void)postJSONWithBaseURL:(NSString *)URL
                     params:(id)params
                 returnCall:(ReturnBlock)block
{
    AFHTTPRequestOperationManager * manager = [[AFHTTPRequestOperationManager alloc] init];
    [manager POST:URL parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (block) {
            block(responseObject, nil); // 请求成功 返回JSON数据
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (block) {
            block(nil, error); // 请求失败 返回error
        }
    }];
}

@end
